import FlowBox from '@/views/workFlow/components/FlowBox'
import CustomForm from './Form'
import { getPaymentrecords } from '@/api/crm/customer'
import FormPrint from './FormPrint/index.vue'
export default {
  components: {
    FlowBox,
    CustomForm,
    FormPrint
  },
  data() {
    return {
      showAll: false,
      isdisabled: false,
      customer: undefined,
      query: {
        name: undefined,
        area: undefined,
        info: undefined,
        consultTime: undefined,
        channel: undefined,
        managerId: undefined,
        category: undefined,
        status: '1'
      },
      defaultProps: {
        value: 'id',
        children: 'children',
        label: 'fullName',
        disabled: 'selectable'
      },
      list: [],
      listLoading: true,
      multipleSelection: [],
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: 'lastConsultTime'
      },
      formVisible: false,
      trackFormVisible: false,
      transferFormVisible: false,
      backFormVisible: false,
      assignFormVisible: false,
      orderFormVisible: false,
      flowVisible: false,
      channelOptions: [],
      genderOptions: [],
      userList: [],
      categoryOptions: [],
      focusAttentionOptions: [],
      brandOptions: [],
      typeOptions: [{ fullName: '全款', id: '1' }, { fullName: '定金', id: '2' }, { fullName: '保证金', id: '3' }, { fullName: '余款', id: '4' }],
      printFormVisible: false
    }
  },
  computed: {},
  created() {
    this.initData()
    // this.getChannelOptions()
    // this.getGenderOptions()
    this.getUserList()
    // this.getCategoryOptions()
    // this.getFocusAttentionOptions()
    this.$store.dispatch('base/getDictionaryData', { sort: 'Brand' }).then(res => {
      this.brandOptions = res
    })
  },
  methods: {
    handlePrint(id, row) {
      this.printFormVisible = true
      this.$nextTick(() => {
        this.$refs.PrintForm.init(id, row)
      })
    },
    getFocusAttentionOptions() {
      this.$store.dispatch('base/getDictionaryData', { sort: 'FocusAttention' }).then(res => {
        this.focusAttentionOptions = this.isDetail ? res : res.filter(o => o.enabledMark == 1)
      })
    },
    addHandle(row, flowState) {
      let data = {
        id: row.id || '',
        enCode: '#visualDevcustomerpaymentrecords',
        flowId: '593322519398712581',
        formType: 1,
        opType: flowState ? 0 : '-1',
        status: flowState
      }
      this.flowVisible = true
      this.$nextTick(() => {
        this.$refs.FlowBox.init(data)
      })
    },
    getUserList() {
      this.$store.dispatch('base/getUserList').then(res => {
        this.userList = res.filter(o => o.enabledMark == 1 && o.positionId.indexOf('445842201948194053') > -1)
      })
    },
    initData() {
      this.listLoading = true
      let _query = {
        ...this.listQuery,
        ...this.query
      }
      let query = {}
      for (let key in _query) {
        if (Array.isArray(_query[key])) {
          query[key] = _query[key].join()
        } else {
          query[key] = _query[key]
        }
      }
      getPaymentrecords
        .call(this, query)
        .then(res => {
          this.list = res.data.list
          this.total = res.data.pagination.total
          this.listLoading = false
        })
        .catch(() => {
          this.listLoading = false
        })
    },
    handleTabClick() {
      this.initData()
    },
    handleSelectionChange(val) {
      this.multipleSelection = this.reduceSame(val)
    },
    // 数组对象去重
    reduceSame(arr) {
      let obj = {}
      return arr.reduce(function(prev, item) {
        obj[item.id] ? '' : (obj[item.id] = 1 && prev.push(item))
        return prev
      }, [])
    },
    handleDel(id) {},
    assignHandle() {
      if (this.multipleSelection.length > 0) {
        this.assignFormVisible = true
        this.$nextTick(() => {
          this.$refs.AssignForm.init(this.multipleSelection)
        })
      }
    },
    transferHandle() {
      if (this.multipleSelection.length > 0) {
        this.transferFormVisible = true
        this.$nextTick(() => {
          this.$refs.TransferForm.init(this.multipleSelection)
        })
      }
    },
    backHandle() {
      if (this.multipleSelection.length > 0) {
        this.backFormVisible = true
        this.$nextTick(() => {
          this.$refs.BackForm.init(this.multipleSelection)
        })
      }
    },
    addSubFormHandle(id) {
      this.trackFormVisible = true
      this.$nextTick(() => {
        this.$refs.TrackForm.init(id)
      })
    },

    signHandle(row, flowState) {
      let data = {
        id: '',
        enCode: '#visualDevTransition',
        flowId: '334172229149394181',
        formType: 1,
        opType: flowState ? 0 : '-1',
        status: flowState,
        customer: row
      }
      this.flowVisible = true
      this.$nextTick(() => {
        this.$refs.FlowBox.init(data)
      })
    },
    colseFlow(isrRefresh) {
      this.flowVisible = false
      if (isrRefresh) this.reset()
    },

    addOrUpdateHandle(row, flowState) {
      let data = {
        id: row ? row.id : '',
        enCode: '#visualDevcustomerpaymentrecords',
        flowId: '593322519398712581',
        formType: 1,
        opType: flowState ? 0 : '-1',
        status: flowState,
        paymentId: row ? row.id : ''
      }
      this.flowVisible = true
      this.$nextTick(() => {
        this.$refs.FlowBox.init(data)
      })
    },
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: 'lastConsultTime'
      }
      this.initData()
    },
    refresh(isrRefresh) {
      this.formVisible = false
      if (isrRefresh) this.reset()
    },
    reset() {
      for (let key in this.query) {
        if (key != 'status') this.query[key] = undefined
      }
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: 'lastConsultTime'
      }
      this.initData()
    }
  }
}
